package cuongnh.project.japanesediary.vocabulary.ui.view.action.menucontribution.vocabulary.sentences;

import java.util.Set;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Vocabulary;
import cuongnh.project.japanesediary.vocabulary.model.Sentence;
import cuongnh.project.japanesediary.vocabulary.ui.dialog.AddRelatedSentenceDialog;
import cuongnh.project.japanesediary.vocabulary.ui.view.VocabularyExamplesView;

public class AddVocabularyExampleAction implements IViewActionDelegate {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.action.AddVocabularyExample";

	protected IViewPart m_view;
	
	protected TableViewer tableViewer;

	private CommonServices commonServices;

	private Vocabulary vocabulary;
	
	private Set<Sentence> selectedSentences;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	@Override
	public void init(IViewPart view) {
		m_view = view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@Override
	public void run(IAction action) {
		// 履歴ビュー更新
		VocabularyExamplesView view = (VocabularyExamplesView) m_view
				.getAdapter(VocabularyExamplesView.class);
		commonServices = view.getCommonServices();
		vocabulary = view.getCurrentVocabulary();
		AddRelatedSentenceDialog relatedSentenceDialog = new AddRelatedSentenceDialog(
				view.getSite().getShell(), commonServices);
		if (relatedSentenceDialog.open() == Window.OK) {
			selectedSentences = relatedSentenceDialog
					.getSelectedSentences();
			if (!selectedSentences.isEmpty()) {
				// update selected vocabulary to current vocabulary
				Vocabulary currentVocabulary = commonServices
						.getVocabularyDao().getVocabularyWithExamples(
								vocabulary.getVocabularyId());
				currentVocabulary.getVocabularyExamples().addAll(selectedSentences);
				commonServices.getVocabularyDao()
						.update(currentVocabulary);

				for(Sentence selectedSentence : selectedSentences){
					// update current vocabulary for selected vocabulary
					selectedSentence = commonServices.getSentenceDao()
							.getSentenceWithVocabularies(
									selectedSentence.getSentenceId());
					selectedSentence.getVocabularies().add(
							currentVocabulary);
					commonServices.getSentenceDao().update(
							selectedSentence);
				}				
			}
		}
		view.update();
		tableViewer = view.getTblvSentences();
		tableViewer.setSelection(new StructuredSelection(selectedSentences.toArray()));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
	 * .IAction, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {

	}

}
